package components
{
	
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragActions;
	import flash.desktop.NativeDragManager;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class Settings{
		private var panel:SuperPanel = new SuperPanel();
		private var label1:Label = new Label();
		private var label2:Label = new Label();
		private var label3:Label = new Label();
		private var label4:Label = new Label();
		private var label5:Label = new Label();
		private var label6:Label = new Label();
		private var oldAvatar:Image = new Image();
		private var newAvatar:Image = new Image();
		private var input1:TextInput = new TextInput();
		private var input2:TextInput = new TextInput();
		private var input3:TextInput = new TextInput();
		private var input4:TextInput = new TextInput();
		private var button:Button = new Button();
		private var tool:Tool = new Tool();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		private var f:FileReference = new FileReference();
		private var selectAvatar:Boolean = false;
		private var fileName:String = "";
		private var deleteFile:File;
		
		public function Settings(p:SuperPanel){
			panel = p;
			label1.x = 10;
			label1.y = input1.y = 27;
			label1.height = 30;
			label1.text = "Username: ";
			input1.x = 130;
			input1.width = panel.width -150;
			input1.height = 20;
			input1.text = panel.parentApplication.user;
			input1.enabled = false;
			label2.x = 10;
			label2.y = input2.y = 57;
			label2.height = 30;
			label2.text = "*Sifra: ";
			input2.x = 130;
			input2.width = panel.width -150;
			input2.height = 20;
			input2.displayAsPassword = true;
			label3.x = 10;
			label3.y = input3.y = 87;
			label3.height = 30;
			label3.text = "Nova sifra: ";
			input3.x = 130;
			input3.width = panel.width -150;
			input3.height = 20;
			input3.displayAsPassword = true;
			label4.x = 10;
			label4.y = input4.y = 117;
			label4.height = 30;
			label4.text = "Ponovo nova sifra: ";
			input4.x = 130;
			input4.width = panel.width -150;
			input4.height = 20;
			input4.displayAsPassword = true;
			label5.x = oldAvatar.x = 20;
			label5.y = 137;
			label5.height = 30;
			label5.text = "Stara slika: ";
			oldAvatar.y = 155;
			oldAvatar.width = 100;
			oldAvatar.height = 100;
			oldAvatar.source = (panel.parentApplication.avatar!="") ? panel.parentApplication.AVATAR_PATH+panel.parentApplication.avatar : panel.parentApplication.AVATAR_PATH+"nophoto.jpg";
			label6.x = newAvatar.x = 130;
			label6.y = 137;
			label6.height = 30;
			label6.text = "Nova slika: ";
			newAvatar.y = 155;
			newAvatar.width = 100;
			newAvatar.height = 100;
			newAvatar.source = panel.parentApplication.AVATAR_PATH+"draghere.jpg";
			button.x = panel.width - 90;
			button.y = panel.height - 125;
			button.height = 100;
			button.label = "Izmjeni";
			panel.addChild(label1);
			panel.addChild(input1);
			panel.addChild(label2);
			panel.addChild(input2);
			panel.addChild(label3);
			panel.addChild(input3);
			panel.addChild(label4);
			panel.addChild(input4);
			panel.addChild(label5);
			panel.addChild(oldAvatar);
			panel.addChild(label6);
			panel.addChild(newAvatar);
			panel.addChild(button);
			newAvatar.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, acceptDrag);
			newAvatar.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, dragDrop);
			button.addEventListener(MouseEvent.CLICK, onButtonClicked);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
		}
		
		public function acceptDrag(event:NativeDragEvent):void {
			NativeDragManager.dropAction = NativeDragActions.MOVE;
        	NativeDragManager.acceptDragDrop(event.currentTarget as InteractiveObject);
		}
		
		public function dragDrop(event:NativeDragEvent):void { 
			var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			for each (var file:File in dropfiles){
				var date:Date = new Date();
				var dodaj:String = date.fullYear+(date.month+1).toString()+date.date+date.hours+date.minutes+date.seconds+date.milliseconds;
				var destination:File;
				switch (file.extension){
					case "png" :
						destination = File.documentsDirectory.resolvePath(panel.parentApplication.user+dodaj+"."+file.extension);
						deleteFile = destination;
						file.copyTo(destination, true)
						newAvatar.source = file.nativePath;
						fileName = destination.name;
						f = FileReference(destination);
						selectAvatar = true;
						break;
					case "jpg" :
						destination = File.documentsDirectory.resolvePath(panel.parentApplication.user+dodaj+"."+file.extension);
						deleteFile = destination;
						file.copyTo(destination, true)
						newAvatar.source = file.nativePath;
						fileName = destination.name;
						f = FileReference(destination);
						selectAvatar = true;
						break;
					case "gif" :
						destination = File.documentsDirectory.resolvePath(panel.parentApplication.user+dodaj+"."+file.extension);
						deleteFile = destination;
						file.copyTo(destination, true)
						newAvatar.source = file.nativePath;
						fileName = destination.name;
						f = FileReference(destination);
						selectAvatar = true;
						break;
					default:
						tool.createAlert("Greska","Samo jpg, gif, png formati su prihvatljivi!");
				}
			}
		}
		
		public function onResize(e:Event):void{
			input1.width = panel.width -150;
			input2.width = panel.width -150;
			input3.width = panel.width -150;
			input4.width = panel.width -150;
			button.x = panel.width - 90;
			button.y = panel.height - 125;
		}
		
		public function onButtonClicked(e:MouseEvent):void{
           	if(!selectAvatar && input3.text =="" && input4.text =="") tool.createAlert("GRESKA", "Niste napravili nikakve izmjene!");
           		else{
           			service.sql = {type:"SELECT", a1:"k.avatar", from:"korisnik k", where:"username=@"+panel.parentApplication.user+"@ AND password=@"+input2.text+"@"};
					service.addEventListener(service.MYSQL_RESULT, onResult);
					service.startService();
           		}
		}
		
		public function onResult(e:Event):void{
	 		var xmlList:XMLList = new XMLList();
			xmlList = service.xml.children();
	 		if(xmlList.length()>0){
            	if(selectAvatar){
            		var pom:String = "";
            		for(var n:Number=0; n<fileName.length-1; n++){
            			if(fileName.charAt(n)!=".") pom+=fileName.charAt(n); else break;
            		}
            		if(input3.text!="" && input4.text!=""){
            			if(input3.text==input4.text){
 							service1.sql = {type:"UPDATE", tables:"korisnik k", s1:"k.password=@"+input3.text+"@", s2:"k.avatar=@"+fileName+"@", where:"k.username=@"+panel.parentApplication.user+"@" };
	 						service1.addEventListener(service1.MYSQL_RESULT, onResult1);
				 			service1.startService();	
	            		} else tool.createAlert("GRESKA", "Sifre nisu identicne!");
            		} else {
						service1.sql = {type:"UPDATE", tables:"korisnik k", s1:"k.avatar=@"+fileName+"@", where:"k.username=@"+panel.parentApplication.user+"@" };
						service1.addEventListener(service1.MYSQL_RESULT, onResult1);
				 		service1.startService();	            			
            		}
	            } else {
            		if(input3.text!="" || input4.text!=""){
            			if(input3.text==input4.text){
 							service1.sql = {type:"UPDATE", tables:"korisnik k", s1:"k.password=@"+input3.text+"@", where:"k.username=@"+panel.parentApplication.user+"@" };
	 						service1.addEventListener(service1.MYSQL_RESULT, onResult1);
				 			service1.startService();	
            			} else tool.createAlert("GRESKA", "Sifre nisu identicne!");
            		} else tool.createAlert("GRESKA", "Niste napravili nikakve izmjene!"); 
	            }
	 		} else tool.createAlert("GRESKA", "Sifra nije odgovarajuca!");
		}
		
		public function onResult1(event:Event):void{
			tool.createAlert("OK", "Izmjene su uspjesno upisane!");
			f.addEventListener(Event.COMPLETE, completeHandler);
			f.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			f.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   			var request:URLRequest = new URLRequest(panel.parentApplication.UPLOAD_AVATARS_SERVICE)
   			if(selectAvatar) f.upload(request);
		}
		
		public function completeHandler(event:Event):void{
    		tool.createAlert("OK", "Avatar je uspjesno postavljen!");
    		deleteFile.deleteFile();
 			service2.sql = {type:"DELETEAVATAR", file:panel.parentApplication.avatar };
	 		service2.addEventListener(service1.MYSQL_RESULT, onResult2);
			service2.startService();
    		panel.parentApplication.avatar = fileName;
    		panel.parentApplication.glavniPanel.refreshAvatar(fileName);  
    		panel.closePanel(); 			
		}	
		
		public function onResult2(event:Event):void{
			trace(service2.xml);
		}
		
		public function ioErrorHandler(event:IOErrorEvent):void{
			trace(event.text);
    		tool.createAlert("GRESKA", "Dogodila se greska prilikom uploada!");
		}	
			
		public function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }
	}
}